package com.cskaoyan.controller.wxcontroller;

import com.cskaoyan.bean.po.MarketCoupon;
import com.cskaoyan.bean.po.MarketCouponUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.wxservice.CouponWeChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 小黑球
 * @datetime 2023/1/1 11:30
 * @description: TODO:
 */
@RestController
@RequestMapping("wx/coupon")
public class CouponWeChatController {

    @Autowired
    CouponWeChatService couponWeChatService;

    /**
     * 个人中心的优惠券列表的方法
     *
     * @param info
     * @param status
     * @return
     */
    @RequestMapping("mylist")
    public BaseRespVo mylist(BasePageInfo info, Short status) {
        CommonData<MarketCoupon> data = couponWeChatService.mylist(info, status);
        return BaseRespVo.ok(data);
    }

    /**
     * 首页的优惠券的列表的方法
     *
     * @param
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info) {
        //看响应结果写返回值类型
        CommonData<MarketCoupon> data = couponWeChatService.list(info);
        return BaseRespVo.ok(data);
    }

    /**
     * 购买商品时选择优惠券的方法
     *
     * @param cartId
     * @param grouponResultId
     * @return
     */
    @RequestMapping("selectlist")
    public BaseRespVo selectlist(Integer cartId, Integer grouponResultId) {
        CommonData<MarketCoupon> data=couponWeChatService.selectList(cartId,grouponResultId);
        return BaseRespVo.ok(data);
    }

    /**
     * 首页的优惠券的领取的方法
     *
     * @param marketCouponUser
     * @return
     */
    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody MarketCouponUser marketCouponUser) {
        int code = couponWeChatService.receive(marketCouponUser.getCouponId());
        if (code == 0) {
            return new BaseRespVo(null, "优惠券已经领取过了", 740);
        }
        return BaseRespVo.ok();
    }

    /**
     * 个人中心的优惠券的优惠券兑换码的方法
     *
     * @param marketCoupon
     * @return
     */
    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody MarketCoupon marketCoupon) {
        int code = couponWeChatService.exchange(marketCoupon.getCode());
        if (code == 742) {
            return new BaseRespVo(null, "优惠券不正确", 742);
        }
        if (code == 740) {
            return new BaseRespVo(null, "优惠券已过期", 740);
        }
        return BaseRespVo.ok();
    }


}
